/* 
 * File:   pic24e_timer.c
 * Author: apleilx
 *
 * Created on 2015?9?3?, ??9:59
 */
#define _TIMER_MODULE_
#include "pincfg.h"
#include "systimer.h"

void SysTick_Handler( void )
{
    SysTickCnt ++;
	SysTickIntCnt ++;
	TickIntHook();
}

/******************************************************************************/
/*********************************Systimer Init********************************/
void SysTickInit()
{
	SysTick_Config((SystemCoreClock / 1000) * SYSTICK_CYCLE);
	NVIC_SetPriority(SysTick_IRQn, 0x2);

	SysPulseClear();
	SysTimerCounterClear();
}

/*****************************************************************************/
/*********************************Systimer??**********************************/
/*
 *
 */

void SysPulseCreat()
{
      uint16_t TickCntTemp;
    
	if(SysTickCnt == 0)
		return;
	
    TickCntTemp = SysTickCnt;
    SysTickCnt  = 0;

    Counter_10ms  += TickCntTemp;
    Counter_50ms  += TickCntTemp;
    Counter_100ms += TickCntTemp;
    Counter_125ms += TickCntTemp;
	Counter_250ms += TickCntTemp;
	Counter_500ms += TickCntTemp;
    Counter_1s    += TickCntTemp;
	 Counter_11s    += TickCntTemp;
    Counter_2s    += TickCntTemp;
    Counter_3s    += TickCntTemp;
    Counter_5s    += TickCntTemp;
    Counter_10s   += TickCntTemp;
    Counter_60s   += TickCntTemp;

    bPulseSysTick   = 1;

    if(Counter_10ms >= COUNTER_10MS)
    {
        Counter_10ms -= COUNTER_10MS;
        bPulse_10ms = 1;
    }
    if(Counter_50ms >= COUNTER_50MS)
    {
        Counter_50ms -= COUNTER_50MS;
        bPulse_50ms = 1;
    }
    if(Counter_100ms >= COUNTER_100MS)
    {
        Counter_100ms -= COUNTER_100MS;
        bPulse_100ms = 1;
        if(PowerUpTimeCnt < 0xFFFFFFFF)
            PowerUpTimeCnt++;
    }
    if(Counter_125ms >= COUNTER_125MS)
    {
        Counter_125ms -= COUNTER_125MS;
        bPulse_125ms = 1;
    }
	if(Counter_250ms >= COUNTER_250MS)
    {
        Counter_250ms -= COUNTER_250MS;
        bPulse_250ms = 1;
    }
	if(Counter_500ms >= COUNTER_500MS)
    {
        Counter_500ms -= COUNTER_500MS;
        bPulse_500ms = 1;
    }
    if(Counter_1s >= COUNTER_1S)
    {
        Counter_1s -= COUNTER_1S;
        bPulse_1s = 1;
    }
		 if(Counter_11s >= COUNTER_11S)
    {
        Counter_11s -= COUNTER_11S;
        bPulse_11s = 1;
    }
    if(Counter_2s >= COUNTER_2S)
    {
        Counter_2s -= COUNTER_2S;
        bPulse_2s = 1;
    }
    if(Counter_3s >= COUNTER_3S)
    {
        Counter_3s -= COUNTER_3S;
        bPulse_3s = 1;
    }
    if(Counter_5s >= COUNTER_5S)
    {
        Counter_5s -= COUNTER_5S;
        bPulse_5s = 1;
    }
    if(Counter_10s >= COUNTER_10S)
    {
        Counter_10s -= COUNTER_10S;
        bPulse_10s = 1;
    }
    if(Counter_60s >= COUNTER_60S)
    {
        Counter_60s -= COUNTER_60S;
        bPulse_60s = 1;
    }
}
/*********************************Systimer**********************************/
/*
 * 
 */
void SysPulseClear()
{
    bPulseSysTick = 0;
    bPulse_10ms  = 0;
    bPulse_50ms  = 0;
    bPulse_100ms = 0;
    bPulse_125ms = 0;
	bPulse_250ms = 0;
	bPulse_500ms = 0;
    bPulse_1s    = 0;
	 bPulse_11s    = 0;
    bPulse_2s    = 0;
    bPulse_3s    = 0;
    bPulse_5s    = 0;
    bPulse_10s   = 0;
    bPulse_60s   = 0;
}

/*********************************Systimer**********************************/
/*
 * ?????
 */
void SysTimerCounterClear()
{
    Counter_10ms = 0;
	Counter_50ms = 0;
	Counter_100ms = 0;
    Counter_125ms = 0;
	Counter_250ms = 0;
	Counter_500ms = 0;
	Counter_1s = 0;
		Counter_11s = 0;
	Counter_2s = 0;
	Counter_3s = 0;
    Counter_5s = 0;
	Counter_10s = 0;
	Counter_60s = 0;
}
